#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gestion_personel.h"
#include "gestion_malade.h"
malade rchch_mal_id(long idf)
{
    malade mal;
    mal.Id=idf;
    char nom[20];
    char prenom[20];
    long int Id=0;
    char type_sang[5];
    char nation[20];
    int num;
    FILE *f;
    f=fopen("malade.txt","r");

    while(fscanf(f,"%s %s %ld %s %s %d",nom,prenom,&Id,type_sang,nation,&num)!=EOF)
    {
        if(Id==idf)
        {
            strcpy(mal.nom,nom) ;
            strcpy(mal.prenom,prenom);
            mal.Id=Id;
            strcpy(mal.type_sang,type_sang);
            mal.num=num;
            fclose (f);
            return (mal);
        }
        if(Id==0)
        {
            mal.Id=-1;
            fclose (f);
            return (mal);
        }
    }
}
personel rchch_prs_id(long int  idf)
{
   personel p;

    p.Id=idf;
    char nom[20];
    char prenom[20];
    long int Id=0;
    char infection[20];
    char type_sang[5];
    char profession[30];
    char tab_etapes[20];
    FILE *fi;
    fi=fopen("personnel.txt","r");//personnel
    while(fscanf(fi,"%s %s %ld %s %s %s %s",nom,prenom,&Id,infection,type_sang,profession,tab_etapes )!=EOF)
    {
        if(Id==idf)

        {
            strcpy(p.nom,nom);
            strcpy(p.prenom,prenom);
            p.Id=Id;
            strcpy(p.type_sang,type_sang);
            strcpy(p.profession,profession);
            strcpy(p.tab_etapes,tab_etapes);
            strcpy(p.infection,infection);
            fclose(fi);
            return (p);
        }
        if(Id==0)
        {
            p.Id=-1;
            fclose(fi);
            return (p);
        }
    }
}

char id_cap[14][6];

int indice(char s[])
{
    int i;
    for(i=0; i<14; i++)
    {
        if(s[0]==id_cap[i][0] && s[2]==id_cap[i][2]) return i;
    }
    return -1;
}

void suivi(void)
{
    malade mal;
    personel p;
    int h,m,s;
    long int id;
    char cap[6];
    int L02[1001];
    int Ps[14][1000];
    int M[14][1000];
    int ordre [1000];
    int vis [1000];
    int vis1[1000];
    int a,b,i,j,time,k,l;
    for(k=0;k<1000;k++)
    {
        for (l=0;l<14;l++)
            M[l][k]=0;
    }
    for(k=0;k<1000;k++)
        ordre[k]=0;
        strcpy(id_cap[0],"E01");
        strcpy(id_cap[1],"C01");
        strcpy(id_cap[2],"S01");
        strcpy(id_cap[3],"L01");
        strcpy(id_cap[4],"L02");
        strcpy(id_cap[5],"C02");
        strcpy(id_cap[6],"V01");
        strcpy(id_cap[7],"I01");
        strcpy(id_cap[8],"D01");
        strcpy(id_cap[9],"B01");
        strcpy(id_cap[10],"M01");
        strcpy(id_cap[11],"Sout");
        strcpy(id_cap[12],"Pout");
        strcpy(id_cap[13],"Ebola");
	
    FILE *f1;
    FILE *f3;
    FILE *f2;
    FILE *f4;
    f1=fopen("events.txt","r");
   
    


    while(fscanf(f1,"%d:%d:%d %ld %s",&h,&m,&s,&id,cap)!=EOF)
    {
        if( (id / 10000)!=1)
        {
            i=indice(cap);
            j=id-10000;
            vis [j]=1;
            time=h*3600+m*60+s;
            if(L02[j]!=-1)
            {
                if(time-L02[j] >= 5*60)
                {
                    Ps[4][j]=1;
                }
                L02[j]=-1;
                if(i>0 && Ps[i-1][j]==0)
                {
                    ordre[j]=1;
                }
                Ps[i][j]=1;
            }
            else//if(L02[j]!=-1)
            {
                if(i==4)
                {
                    L02[j]=time;
                }
                else
                {
                    Ps[i][j]=1;
                    if((i>=0 && i<=8) && Ps[i-1][j]==0)
                    {
                        ordre[j]=1;
                    }
                    if(Ps[13][j]==1)
                    {
                        p=rchch_prs_id(id);
			
                         f3=fopen("ebola.txt","w");
                        fprintf(f3,"la personne %s %s de %ld est probablement malade\n",p.nom, p.prenom,id);

                    }
                }
            }//if(L01[j]!=-1)
        }//if( (id % 1000)!=3)
        else
        {
            i=indice(cap);
            j=id-10000;
            vis[j]=1;
            M[i][j]=1;
            if (M[i][j]=1)
            {
                mal=rchch_mal_id(id);
		
   		 f4=fopen("suivi1.txt","w");
                fprintf(f4,"le malade %s %s %ld ne respecte pas les normes dans la zone \n",mal.nom,mal.prenom,id);
            }
        }//if( (id % 10000)!=3)
    }//while
    for(a=0; a<1000; a++)

    {
        if(ordre[a]==1)
        {
            p=rchch_prs_id(id);
	     
             f2=fopen("suivi.txt","w");
            fprintf(f2,"la personne %s %s de %ld n'a pas respecte les normes de securite\n",p.nom, p.prenom,id);
        }
    }

    fclose (f1);
    fclose(f2);
    fclose(f2);
    fclose(f3);
    fclose (f4);

}




